package chap09;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class EchoMultiServerThread extends Thread {
    public static void main(String[] args) {

    }
    private Socket socket = null;
    public EchoMultiServerThread(Socket socket)
    {
        super("EchoMultiServerThread");
        this.socket=socket;
    }
    public void run(){
        try{
        PrintWriter out=null;   //输出流
        BufferedReader in =null;//输入流
        out=new PrintWriter(socket.getOutputStream(),true);
        in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//get inputstream in socket
            out.println("hello!!!");
            out.println("enter to bye");
            out.flush();
            while(true){
                String str=in.readLine();
                if(str==null)
                {
                    break;
                }else
                {
                    out.println("Echo:"+str);
                    out.flush();
                    if (str.trim().equalsIgnoreCase("BYE")) {
                        break;
                    }
                }

            }
            out.close();
            in.close();
            socket.close();

        }catch(IOException e){
            e.printStackTrace();
        }
    }

}